package com.demo.project_template.controller;

import com.demo.project_template.constant.CommonConstants;
import com.demo.project_template.entity.ShoppingCart;
import com.demo.project_template.service.ShoppingCartService;
import com.demo.project_template.util.RedisUtil;
import com.demo.project_template.vo.request.shoppingcart.*;
import com.demo.project_template.vo.response.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * @author yyl25
 */
@RestController
@Api(tags = "购物车模块")
@RequestMapping("/api/shoppingCart")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;
    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("添加到购物车")
    @PostMapping("/addShoppingCart")
    public R<?> addShoppingCart(@RequestBody @NotBlank CartAddVO cartAddVo) {
        shoppingCartService.addShoppingCart(cartAddVo);
        return R.success();
    }

    @ApiOperation("查询用户购物车列表")
    @PostMapping("/selectShoppingCartList")
    public R<SelectShoppingCartListVO> selectShoppingCartList(@RequestParam @NotNull Long userId) {
        return R.success(shoppingCartService.selectShoppingCartList(userId));
    }

    @ApiOperation("购物车全选")
    @PostMapping("/selectAllCart")
    public R<SelectShoppingCartListVO> selectAllCart(@RequestParam @NotNull Long userId) {
        return R.success(shoppingCartService.selectAllCart(userId));
    }

    @ApiOperation("购物车全不选")
    @PostMapping("/unSelectAllCart")
    public R<SelectShoppingCartListVO> unSelectAllCart(@RequestParam @NotNull Long userId) {
        return R.success(shoppingCartService.unSelectAllCart(userId));
    }

    @ApiOperation("修改购物车")
    @PostMapping("/updateShoppingCart")
    public R<List<ShoppingCart>> updateShoppingCart(@RequestBody @NotBlank CartUpdateVO cartUpdateForm) {
        return R.success(shoppingCartService.updateShoppingCart(cartUpdateForm));
    }

    @ApiOperation("删除购物车")
    @PostMapping("/deleteShoppingCart")
    public R<List<ShoppingCart>> deleteShoppingCart(@RequestBody @NotBlank DeleteShoppingCartVO deleteShoppingCartList) {
        return R.success(shoppingCartService.deleteShoppingCart(deleteShoppingCartList));
    }

    @ApiOperation("查询用户购物车中选中的商品")
    @PostMapping("/listForCartChecked")
    public R<List<ListForCartCheckedVO>> listForCartChecked(@RequestParam @NotNull Long userId) {
        return R.success(shoppingCartService.listForCartCheckedProduct(userId));
    }

    @ApiOperation("删除用户购物车选中的商品")
    @PostMapping("/deleteCartChecked")
    public R<?> deleteCartChecked(@RequestParam @NotNull Long userId){
        shoppingCartService.deleteCartChecked(userId);
        return R.success();
    }

    @ApiOperation("删除用户购物车所有商品")
    @PostMapping("/deleteCartAll")
    public R<?> deleteCartAll(@RequestParam @NotNull Long userId){
        String redisKey = String.format(CommonConstants.CART_REDIS_KEY_TEMPLATE, userId);
        redisUtil.del(redisKey);
        return R.success();
    }

    @ApiOperation("删除所有用户购物车")
    @GetMapping("/deleteAllUsersCart")
    public R<?> deleteAllUsersCart(){
        shoppingCartService.deleteAllUsersCart();
        return R.success();
    }
}
